<?php

class Page extends Frontend_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('page_m');
    }

    public function index() {
        // Fetch the page template
        $this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE); // biến true cuối cùng để trả về một object chứ ko phải 1 array
        // nếu ko trả về page nào thì show lỗi
        count($this->data['page']) || show_404(current_url());
        add_meta_title($this->data['page']->title);
        // Fetch the page data
        $method = '_' . $this->data['page']->template;
        // nếu method tồn tại
        if (method_exists($this, $method)) {
            $this->$method();
        }
        // nếu method ko tồn tại
        else {
            log_message('error', 'Could not load template ' . $method . ' in file ' . __FILE__ . ' at line ' . __LINE__);
            show_error('Could not load template ' . $method);
        }

        // Load the view
        $this->data['subview'] = $this->data['page']->template;
        // data sẽ truyền cái menu kế thừa từ Frontend
        $this->load->view('_main_layout', $this->data);
    }

    public function _page() {
        $this->data['recent_news'] = $this->article_m->get_recent();
    }

    public function _homepage() {
        $this->data['recent_news'] = $this->article_m->get_recent();
        $this->article_m->set_published();
        $this->db->limit(6);
        $this->data['articles'] = $this->article_m->get();
    }

    public function _news_archive() {
        $this->data['recent_news'] = $this->article_m->get_recent();
        // Count all articles
        $this->article_m->set_published();
        $count = $this->db->count_all_results('articles');

        // Set up pagination
        $perpage = 4; // chỉ hiện 4 bài trên 1 trang
        if ($count > $perpage) {
            $this->load->library('pagination');
            $config['base_url'] = site_url($this->uri->segment(1) . '/');
            $config['total_rows'] = $count;
            $config['per_page'] = $perpage;
            $config['uri_segment'] = 2;
            $this->pagination->initialize($config);
            $this->data['pagination'] = $this->pagination->create_links();
            $offset = $this->uri->segment(2);
        }
        // nếu ko cần phân trang <1, 2, 3 ... >
        else {
            $this->data['pagination'] = '';
            $offset = 0;
        }

        // Fetch articles
        $this->article_m->set_published();
        $this->db->limit($perpage, $offset);
        $this->data['articles'] = $this->article_m->get();
    }

}

?>
